<?php

/**
 * Model CustomerBookForm
 *
 * @author nguyenmanhluu
 * @since Aug 12, 2013 - 9:24:38 PM
 */
class CustomerBookForm extends Users
{

    function rules()
    {
        $rules[] = array(
            'firstname, lastname, mobile_phone, email',
            'required'
        );
        $rules[] = array('mobile_phone', 'numerical');
        $rules[] = array('mobile_phone', 'check_start');
        $rules[] = array(
            'address, postal_code',
            'required', 'on' => 'validate_address'
        );
        $rules[] = array('email', 'email');
        $rules[] = array('email', 'check_email');
        return $rules;
    }

    public function check_start($attribute, $params)
    {
        $arr = array('6', '9', '8');
        if ($this->mobile_phone != '')
        {
            $a = $this->mobile_phone;
            if (!in_array($a[0], $arr))
            {
                $this->addError($attribute, Yii::t('main', 'Please provide a valid phone number'));
            }
            return;
        }
    }

    public function check_email($attribute, $params)
    {

        if (!$this->check_duplicate())
        {
            $this->addError($attribute, Yii::t('main', 'Email has already been registered'));
            return;
        }
    }

    public function check_duplicate()
    {
        if ($this->isNewRecord)
        {

            $crite            = new CDbCriteria();
            $crite->condition = 'email=:email AND type=:type';
            $crite->params    = array(':email' => $this->email, ':type' => Users::TYPE_CUSTOMER);

            $user = Users::model()->find($crite);

            if ($user)
            {
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
    }

}

/** 
 * End file UserSignUp
 */
